package io.zhiller.ollamaclient.ui.component

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.Visibility
import androidx.compose.material.icons.filled.VisibilityOff
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp

@Composable
fun SearchTextField(
  isSearchButton:MutableState<Boolean>,
  searchQuery: MutableState<String>,
  modifier: Modifier = Modifier,
  onSearch: (String) -> Unit,
) {
  TextField(
    value = searchQuery.value,
    onValueChange = { newValue ->
      searchQuery.value = newValue
    },
    shape = RoundedCornerShape(12.dp),
    colors = TextFieldDefaults.colors().copy(
      focusedIndicatorColor = Color.Transparent,
      unfocusedIndicatorColor = Color.Transparent,
      disabledIndicatorColor = Color.Transparent
    ),
    modifier = modifier,
    placeholder = { Text("输入将要搜索的内容...") },
    trailingIcon = {
      Row(
        verticalAlignment = Alignment.CenterVertically
      ) {
        if (searchQuery.value.isNotEmpty()) {
          IconButton(
            
            onClick = { searchQuery.value = "" }, modifier = Modifier.padding(end = 8.dp)
          ) {
            Icon(
              imageVector = Icons.Default.Close, contentDescription = "清除"
            )
          }
        }
        Button(
          enabled = isSearchButton.value,
          onClick = { onSearch(searchQuery.value) },
          modifier = Modifier.padding(end = 8.dp),
          shape = RoundedCornerShape(12.dp)
        ) {
          Icon(
            imageVector = Icons.Default.Search, contentDescription = "搜索"
          )
        }
      }
    },
    keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),
    keyboardActions = KeyboardActions(onSearch = { onSearch (searchQuery.value)}),
    singleLine = true
  )


//    BasicTextField(
//      value = searchQuery,
//      onValueChange = {
//        searchQuery = it
//        if (it.isNotEmpty()) {
//          onSearch(it)
//        }
//      },
//      modifier = modifier
//        .background(Color.Transparent)
//        .fillMaxWidth()
//        .clip(RoundedCornerShape(12.dp)),
//      decorationBox = { innerTextField ->
//        Box(
//          modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.CenterStart
//        ) {
//          innerTextField()
//          if (searchQuery.isNotEmpty()) {
//            IconButton(
//              onClick = { searchQuery = "" },
//              modifier = Modifier
//                .align(Alignment.CenterEnd)
//                .padding(end = 8.dp)
//            ) {
//              Icon(
//                imageVector = Icons.Default.Close, contentDescription = "清除"
//              )
//            }
//          }
//        }
//      },
//      keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),
//      keyboardActions = KeyboardActions(onSearch = { }),
//      singleLine = true
//    )

}

@Composable
fun InputTextField(modifier: Modifier = Modifier,title:String,placeholder: String,input:MutableState<String>) {
  TextField(
    value = input.value,
    onValueChange = { newValue ->
      input.value = newValue
    },
    shape = RoundedCornerShape(12.dp),
    colors = TextFieldDefaults.colors().copy(
      focusedIndicatorColor = Color.Transparent,
      unfocusedIndicatorColor = Color.Transparent,
      disabledIndicatorColor = Color.Transparent
    ),
    label = { Text(title) },
    modifier = modifier.background(Color.Transparent),
    placeholder = { Text(placeholder) },
    trailingIcon = {
      Row(
        verticalAlignment = Alignment.CenterVertically
      ) {
        if (input.value.isNotEmpty()) {
          IconButton(
            onClick = { input.value = "" }, modifier = Modifier.padding(end = 8.dp)
          ) {
            Icon(
              imageVector = Icons.Default.Close, contentDescription = "清除"
            )
          }
        }
      }
    },
    singleLine = true
  )
}

@Composable
fun PasswordTextField(
  modifier: Modifier = Modifier,
  title: String,
  placeholder: String,
  input: MutableState<String>
) {
  // 状态：控制密码是否可见
  val passwordVisible = remember { mutableStateOf(false) }
  
  TextField(
    value = input.value,
    onValueChange = { newValue ->
      input.value = newValue
    },
    shape = RoundedCornerShape(12.dp),
    colors = TextFieldDefaults.colors().copy(
      focusedIndicatorColor = Color.Transparent,
      unfocusedIndicatorColor = Color.Transparent,
      disabledIndicatorColor = Color.Transparent
    ),
    label = { Text(title) },
    modifier = modifier.background(Color.Transparent),
    placeholder = { Text(placeholder) },
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password), // 设置键盘类型为密码
    visualTransformation = if (passwordVisible.value) {
      VisualTransformation.None // 密码可见
    } else {
      PasswordVisualTransformation() // 密码不可见
    },
    trailingIcon = {
      Row(
        verticalAlignment = Alignment.CenterVertically
      ) {
        // 显示/隐藏密码按钮
        IconButton(
          onClick = { passwordVisible.value = !passwordVisible.value }
        ) {
          Icon(
            imageVector = if (passwordVisible.value) {
              Icons.Default.VisibilityOff // 密码可见时显示隐藏图标
            } else {
              Icons.Default.Visibility // 密码不可见时显示显示图标
            },
            contentDescription = if (passwordVisible.value) {
              "隐藏密码"
            } else {
              "显示密码"
            }
          )
        }
      }
    },
    singleLine = true
  )
}